package com.manage.platform.utils;

import com.manage.platform.vo.IndicatorSystemTreeOneVo;
import com.manage.platform.vo.IndicatorSystemTreeVo;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 组织树形结构工具类
 */
public class TreeListIndicatorSystemUtil {

    /**
     * 构建树形结构
     * @param list 所有组织节点列表
     * @return 树形结构列表
     */
    /**
     *
     */
    public static List<IndicatorSystemTreeVo> buildMenuTreeList(List<IndicatorSystemTreeVo> list) {
        try {
            // 使用Set去重
            Set<Long> seenIds = new HashSet<>();
            List<IndicatorSystemTreeVo> uniqueList = new ArrayList<>();

            for (IndicatorSystemTreeVo node : list) {
                if (seenIds.add(node.getId())) {
                    uniqueList.add(node);
                }
            }
            // 使用LinkedHashMap保持插入顺序
            Map<Long, IndicatorSystemTreeVo> nodeMap = new LinkedHashMap<>();
            for (IndicatorSystemTreeVo node : uniqueList) {
                nodeMap.put(node.getId(), node);
            }
            List<IndicatorSystemTreeVo> rootNodes = new ArrayList<>();

            for (IndicatorSystemTreeVo node : uniqueList) {
                Long parentId = node.getParentId();
                if (parentId == null || parentId.equals(0L)) {
                    rootNodes.add(node);
                } else {
                    IndicatorSystemTreeVo parent = nodeMap.get(parentId);
                    if (parent != null) {
                        if (parent.getChildren() == null) {
                            parent.setChildren(new ArrayList<>());
                        }
                        parent.getChildren().add(node);
                    }else {
                        rootNodes.add(node);
                    }
                }
            }
            return rootNodes;
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }


    /**
     *
     */
    public static List<IndicatorSystemTreeOneVo> buildMenuTreeListOne(List<IndicatorSystemTreeOneVo> list) {
        try {
            // 使用Set去重
            Set<Long> seenIds = new HashSet<>();
            List<IndicatorSystemTreeOneVo> uniqueList = new ArrayList<>();

            for (IndicatorSystemTreeOneVo node : list) {
                if (seenIds.add(node.getId())) {
                    uniqueList.add(node);
                }
            }
            // 使用LinkedHashMap保持插入顺序
            Map<Long, IndicatorSystemTreeOneVo> nodeMap = new LinkedHashMap<>();
            for (IndicatorSystemTreeOneVo node : uniqueList) {
                nodeMap.put(node.getId(), node);
            }
            List<IndicatorSystemTreeOneVo> rootNodes = new ArrayList<>();

            for (IndicatorSystemTreeOneVo node : uniqueList) {
                Long parentId = node.getParentId();
                if (parentId == null || parentId.equals(0L)) {
                    rootNodes.add(node);
                } else {
                    IndicatorSystemTreeOneVo parent = nodeMap.get(parentId);
                    if (parent != null) {
                        if (parent.getChildren() == null) {
                            parent.setChildren(new ArrayList<>());
                        }
                        parent.getChildren().add(node);
                    }else  {
                        rootNodes.add(node);
                    }
                }
            }
            return rootNodes;
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

}

